<?php

/*
 * ************************************************* 
 * Created on :2012-5-5 23:09:28
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class forum extends Controller {

    public function init() {
        $this->selfc = 'fengcai';
        $this->tpl->assign("title", "河北工业大学校友会-校友风采");
    }

    function index() {
        $key = empty($_GET['key']) ? "" : $_GET['key'];

        $forum_page_size = 20;

        $n = Forumc::postscount($key);
        list($paging, $offset) = Toolfunc::paging($n, $forum_page_size);
        $this->tpl->assign("paging", $paging);
        $posts = Forumc::posts($key, $offset, $forum_page_size);
        foreach ($posts as $k => $v) {
            $posts[$k]['u'] = $this->user->byid($v['userid']);
            $posts[$k]['reply'] = Forumc::getreplayscount($v['id']);
        }
        $this->tpl->assign("js_set", json_encode(array("key" => $key)));
        $this->tpl->assign("posts", $posts);
        $this->tpl->display("forum/index.html");
    }

    function each() {
        if (empty($_GET['id'])) {
            header("location:/forum/");
            return;
        }
        $id = $_GET['id'];
        $r = Forumc::getone($id);
        if (!$r) {
            header("location:/forum/");
            return;
        }
        $r['u'] = $this->user->byid($r['userid']);
        $this->tpl->assign("each", $r);

        $forum_page_size = 2;

        $n = Forumc::getreplayscount($id);
        list($paging, $offset) = Toolfunc::paging($n, $forum_page_size);
        $this->tpl->assign("paging", $paging);
        $replys = Forumc::getreplys($id, $offset, $forum_page_size);
        foreach ($replys as $k => $v)
            $replys[$k]['u'] = $this->user->byid($v['userid']);
        $this->tpl->assign("replys", $replys);
        $this->tpl->display("forum/each.html");
    }

    function post() {
        if (!empty($_POST)) {
            if (!empty($_POST['parent_id'])) {
                Forumc::add('', $_POST['content'], "", $_POST['parent_id']);
                $id = $_POST['parent_id'];
            } else {
                $r = Forumc::add($_POST['title'], $_POST['content'], '', NULL);
                $id = $r;
            }
            header("location:/forum/each?id=" . $id);
            die();
        }
        $this->tpl->display("forum/post.html");
    }

}

/* End of file forum */


